import {
  getCompany,
  getV2FruitList,
  getFruitList,
  getCollections,
  collectionConfigsEdit,
  collectionConfigsEditV3,
  myFruitDetail,
  collectionConfigsDetail,
  collectionConfigsDetailV3
} from '../../../utils/api/index.js';
import {
  host
} from '../../../utils/status.js';
import Toast from '../../../vant/toast/toast';
Page({
  data: {
    title: "新增揽收点",
    configId: "",
    companyArray: [], // 产区列表
    companyId: "", // 当前产区id
    companyInfo: {}, // 选择的产区信息
    fruitList: [], // 产区下水果列表
    fruitId: "", // 当前水果id
    fruitInfo: {}, // 选择的水果信息
    collectionList: [], // 水果下揽收点列表
    collectionId: '', // 选择的揽收点id
    collectionPoint: {} // 选择的揽收点信息
  },

  onLoad: function (options) {
    const configId = options.configId
    const companyId = options.companyId
    const fruitId = options.fruitId

    if (options.configId != 0) {
      this.setData({
        configId: configId,
        companyId: companyId,
        fruitId: fruitId,
        title: "编辑揽收点"
      })
      this.getData()
    }
    // 生命周期函数--监听页面加载
    this.getCompanyList();
  },

   // 我的水果详情
   getData() {
    collectionConfigsDetailV3({
      companyId: this.data.companyId,
      fruitId: this.data.fruitId
    }).then(res => {
      if (res.resultStatus) {
        this.setData({
          configId: res.resultData.configId,//主键id
          fruitId: res.resultData.fruitId,
          companyId: res.resultData.companyId,
          companyInfo: {
            companyId: res.resultData.companyId,
            companyName: res.resultData.companyName,
          },
          collectionId: res.resultData.collectionId,
          collectionPoint: {
            name: res.resultData.collectionName,
            collectionId: res.resultData.collectionId
          }
        })
        this.getFruitList()
      }
    })
  },

  // 获取分公司列表
  getCompanyList() {
    getCompany().then((res) => {
      if (res.resultStatus) {
        const companyArray = res.resultData.dataList;
        this.setData({
          companyArray: companyArray
        });
      }
    });
  },
   // 产区切换时
   collectionChange(e) {
    const key = e.detail.value
    if(this.data.companyArray[key].companyId !== this.data.companyId){
      this.setData({
        companyId: '',
        companyInfo: {},
        fruitList: [],
        fruitId: '',
        fruitInfo: {},
        collectionList: [],
        collectionId: '',
        collectionPoint: {}
      })
    }
    this.setData({
      companyInfo: {
        companyName: this.data.companyArray[key].companyName,
        companyId: this.data.companyArray[key].companyId,
      },
      companyId: this.data.companyArray[key].companyId
    })
    this.getFruitList()
  },
  /**
   * 获取水果信息列表
   */
  getFruitList() {
    getV2FruitList({
      companyId: this.data.companyId
    })
    .then(res => {
      if (res.resultStatus) {
        const fruitList = res.resultData.map(item => {
          if (item.fruitId == this.data.fruitId) {
            item.checked = true
          } else {
            item.checked = false
          }
          return item
        });
        this.setData({
          fruitList
        });
        if (this.data.configId != 0) {
          this.Changefruit({ detail: { value: this.data.fruitId } })
        }
      }
    });
  },
  // 选择水果时
  Changefruit(e) {
    if(e.detail.value !== this.data.fruitId){
      this.setData({
        fruitId: '',
        fruitInfo: {},
        collectionList: [],
        collectionId: '',
        collectionPoint: {} //揽收点清空
      })
    }
    const fruitId = e.detail.value
    const fruitInfo = this.data.fruitList.find(item => item.fruitId == fruitId) || {}
    this.setData({
      fruitId,
      fruitInfo
    })
    this.getCollectionList()
  },
  // 获取揽收点信息列表
  getCollectionList() {
    const fruitId = this.data.fruitId;
    const companyId = this.data.companyId;
    getCollections({
      fruitId: fruitId,
      companyId: companyId,
    }).then((res) => {
      if (res.resultStatus) {
        const collectionList = res.resultData;
        this.setData({
          collectionList: collectionList.map(item => {
            if (item.collectionId == this.data.collectionId) {
              item.checked = true
            } else {
              item.checked = false
            }
            return item
          })
        });
      }
    });
  },
  // 揽收点切换
  pointChange(e) {
    const collectionId = e.detail.value
    const collectionPoint = this.data.collectionList.find(item => item.collectionId == collectionId) || {}
    this.setData({
      collectionId,
      collectionPoint
    })
  },

  pointChangeTwo (e) {
    const data = e.currentTarget.dataset.fruit
    const collectionId = data.collectionId
    const collectionPoint = data
    const collectionList = this.data.collectionList.map(f => {
      if (f.collectionId === collectionId) {
        f.checked = true
      } else {
        f.checked = false
      }
      return f
    })
    this.setData({
      collectionId,
      collectionPoint,
      collectionList
    })
  },
  // 提交
  submit(e) {
    const obj = {
      configId:this.data.configId || 0,
      companyId: this.data.companyId,
      fruitId: this.data.fruitId,
      collectionId: this.data.collectionId
    }
    if (!obj.companyId) {
      wx.showToast({ title: '请选择产区', icon: 'none' });
      return false;
    }
    if (!obj.fruitId) {
      wx.showToast({ title: '请选择水果', icon: 'none' });
      return false;
    }
    if (!obj.collectionId) {
      wx.showToast({ title: '请选择揽收点', icon: 'none' });
      return false;
    }

    collectionConfigsEditV3(obj).then(res => {
      if (res.resultStatus) {
        wx.showToast({
          icon: 'success',
          title: '操作成功',
        })

        wx.navigateBack({
          delta: 1
        })
      } else {
        Toast(res.errorMessage);
      }
    })
  }
})